#!/usr/bin/env python
"""
Nagios plugin that monitors the Grid Information System. Both GRIS (Grid Resource
Information Service/Server) and GIIS (Grid Information Index Service/Server) can
be monitored out of the box.

Last change:  bdii based infosys (for ARC 0.8.2)
"""

__author__ = "Placi Flury grid@switch.ch"
__copyright__ = "Copyright 2010, SMSCG an AAA/SWITCH project"
__date__ = "27.9.2010"
__version__ = "0.3.1"
__last_change__="30.11.11" 

# last modification: ARC1 adaptations 30.11.2011


import sys
from nagios_plugin import NagiosPluginBasic, LocalService


class GridInfosys(NagiosPluginBasic):
    """
    Checks whether GridInfosysservice is running. It's a local check
    without any connectivity testing.
    """
    def __init__(self):
        parser = NagiosPluginBasic.getOptions(self)
        parser.remove_option("-c")   # removing thresholds
        parser.remove_option("-w")
        parser.remove_option("-H")   # remove hostname option
        parser.parse_args()


    def main(self):
        
        service_new = LocalService("bdii-update", pidloc='/var/run/arc/bdii')
        service_old = LocalService("bdii-update")
        if service_new.getpid() >= 0:
            service = service_new
        else:
            service = service_old
            
        msg, return_code = service.getstate()

        print msg
        sys.exit(return_code)


if __name__ == "__main__":
    g = GridInfosys()
    g.main()
